﻿using Microsoft.Win32;
using System.IO;
using System.Windows.Forms;

namespace Abide.Classes
{
    public static class Registration
    {
        public static void RegisterAao()
        {
            //Load Data...
            RegistryKey AaoKey = Registry.CurrentUser.CreateSubKey(@"Software\Classes\.aao");
            RegistryKey AbideAddOn = Registry.CurrentUser.CreateSubKey(@"Software\Classes\Abide AddOn.aao");
            RegistryKey DefaultIcon = AbideAddOn.CreateSubKey("DefaultIcon");
            RegistryKey ShellOpenCommand = AbideAddOn.CreateSubKey(@"shell\open\command");

            //Prepare
            AbideAddOn.SetValue(string.Empty, "Abide AddOn Package");
            ShellOpenCommand.SetValue(string.Empty, string.Format("\"{0}\" \"%1\"", Application.ExecutablePath));
            DefaultIcon.SetValue(string.Empty, Path.Combine(new FileInfo(Application.ExecutablePath).Directory.FullName, @"Icons\Abide AddOn.ico"));

            //Check for Assignment
            AaoKey.SetValue(string.Empty, "Abide AddOn.aao");

            //Close
            AaoKey.Close(); AaoKey.Dispose();
            AbideAddOn.Close(); AbideAddOn.Dispose();
            DefaultIcon.Close(); DefaultIcon.Dispose();
            ShellOpenCommand.Close(); ShellOpenCommand.Dispose();
        }
        public static void RegisterAte()
        {
            //Load Data...
            RegistryKey AteKey = Registry.CurrentUser.CreateSubKey(@"Software\Classes\.ate");
            RegistryKey AbideTypeEditor = Registry.CurrentUser.CreateSubKey(@"Software\Classes\Abide TypeEditor.ate");
            RegistryKey DefaultIcon = AbideTypeEditor.CreateSubKey("DefaultIcon");
            RegistryKey ShellOpenCommand = AbideTypeEditor.CreateSubKey(@"shell\open\command");

            //Prepare
            AbideTypeEditor.SetValue(string.Empty, "Abide Type Editor Package");
            ShellOpenCommand.SetValue(string.Empty, string.Format("\"{0}\" \"%1\"", Application.ExecutablePath));
            DefaultIcon.SetValue(string.Empty, Path.Combine(new FileInfo(Application.ExecutablePath).Directory.FullName, @"Icons\Abide TypeEditor.ico"));

            //Check for Assignment
            AteKey.SetValue(string.Empty, "Abide TypeEditor.ate");

            //Close
            AteKey.Close(); AteKey.Dispose();
            AbideTypeEditor.Close(); AbideTypeEditor.Dispose();
            DefaultIcon.Close(); DefaultIcon.Dispose();
            ShellOpenCommand.Close(); ShellOpenCommand.Dispose();
        }
        public static void RegisterMap()
        {
            //Load Data...
            RegistryKey MapKey = Registry.CurrentUser.CreateSubKey(@"Software\Classes\.map");
            RegistryKey HaloMap = Registry.CurrentUser.CreateSubKey(@"Software\Classes\Abide Halo Map.map");
            RegistryKey DefaultIcon = HaloMap.CreateSubKey("DefaultIcon");
            RegistryKey ShellOpenCommand = HaloMap.CreateSubKey(@"shell\open\command");

            //Prepare
            HaloMap.SetValue(string.Empty, "Halo Map File");
            ShellOpenCommand.SetValue(string.Empty, string.Format("\"{0}\" \"%1\"", Application.ExecutablePath));
            DefaultIcon.SetValue(string.Empty, Path.Combine(new FileInfo(Application.ExecutablePath).Directory.FullName, @"Icons\Halo Map.ico"));

            //Assign
            MapKey.SetValue(string.Empty, "Abide Halo Map.map");

            //Close
            MapKey.Close(); MapKey.Dispose();
            HaloMap.Close(); HaloMap.Dispose();
            DefaultIcon.Close(); DefaultIcon.Dispose();
            ShellOpenCommand.Close(); ShellOpenCommand.Dispose();
        }
        public static void Register(string ext, string progId, string desc, string iconPath, string cmdLine)
        {
            //Set Command Line
            if (string.IsNullOrEmpty(cmdLine))
                cmdLine = string.Format("\"{0}\" \"%1\"", Application.ExecutablePath);
            else if (cmdLine == "null")
                cmdLine = string.Empty;

            //Load Data...
            RegistryKey ExtensionKey = Registry.CurrentUser.CreateSubKey(@"Software\Classes\" + ext);
            RegistryKey ProgIDKey = Registry.CurrentUser.CreateSubKey(@"Software\Classes\" + progId);
            RegistryKey DefaultIcon = ProgIDKey.CreateSubKey("DefaultIcon");
            RegistryKey ShellOpenCommand = ProgIDKey.CreateSubKey(@"shell\open\command");

            //Prepare
            ProgIDKey.SetValue(string.Empty, desc);
            ShellOpenCommand.SetValue(string.Empty, cmdLine);
            DefaultIcon.SetValue(string.Empty, iconPath);

            //Assign
            ExtensionKey.SetValue(string.Empty, progId);

            //Close
            ExtensionKey.Close(); ExtensionKey.Dispose();
            ProgIDKey.Close(); ProgIDKey.Dispose();
            DefaultIcon.Close(); DefaultIcon.Dispose();
            ShellOpenCommand.Close(); ShellOpenCommand.Dispose();
        }
    }
}
